package top.carrotguo.weibo.ui.login;

import android.os.Bundle;
import android.os.Handler;
import android.os.Message;

import com.sina.weibo.sdk.auth.AccessTokenKeeper;
import com.sina.weibo.sdk.auth.Oauth2AccessToken;


import top.carrotguo.weibo.MainActivity;
import top.carrotguo.weibo.R;
import top.carrotguo.weibo.ui.base.BaseActivity;

public class SplashActivity extends BaseActivity {

    /*进入登录界面*/
    private static final int MSG_LOGIN = 1;
    /*进入主界面*/
    private static final int MSG_MAIN = 2;
    /*多少秒后跳转*/
    private static final int MST_DUA = 2000;

    private  Handler mHandler = new Handler() {
        @Override
        public void handleMessage(Message msg) {
            super.handleMessage(msg);
            switch (msg.what) {
                case MSG_LOGIN:
                    readyGoThenKill(LoginActivity.class);
                    break;
                case MSG_MAIN:
                    readyGoThenKill(MainActivity.class);
                    break;
            }
        }
    };

    @Override
    public int provideContentViewId() {
        return R.layout.activity_splash;
    }

    @Override
    public Bundle getBundleExtras(Bundle bundle) {
        return null;
    }

    @Override
    protected void initView() {
        super.initView();
        //判断accessToken是否存在或者过期
        Oauth2AccessToken mAccess = AccessTokenKeeper.readAccessToken(getApplicationContext());
        if (mAccess.isSessionValid()) {
            //有效,进入主界面
            mHandler.sendEmptyMessageDelayed(MSG_MAIN,MST_DUA);
        } else {
            //无效,进入登录界面
            mHandler.sendEmptyMessageDelayed(MSG_LOGIN,MST_DUA);
        }
    }

}
